//
//  CASpringAnimationViewController.m
//  iOSHeartAnimation
//
//  Created by ddy on 2018/7/26.
//  Copyright © 2018年 ddy. All rights reserved.
//
//CASpringAnimation是iOS9才引入的动画类，效果类似于UIView的spring动画，不过比其增加了质量，劲度系数等属性的扩展，继承于CABaseAnimation
#import "CASpringAnimationViewController.h"

@interface CASpringAnimationViewController ()<CAAnimationDelegate>
/** 红色View */
@property (nonatomic, strong) UIView  *redView;
@end
static NSString * const kSpringKey = @"kSpringKey";
static NSString * const kSpringPositionKey = @"kSpringPositionKey";
@implementation CASpringAnimationViewController
/** 红色View */
- (UIView *)redView{
    if(!_redView){
        _redView = [UIView new];
        _redView.layer.cornerRadius = 8.0;
        _redView.layer.masksToBounds = YES;
        _redView.backgroundColor = [UIColor redColor];
    }
    return  _redView;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    self.navigationItem.title = @"CASpringAnimation";
    self.redView.frame = CGRectMake(100, 250, 80, 80);
    [self.view addSubview:self.redView];
    //动画
    [self springAnimation:CGPointMake(100, 250)];
    


}
#pragma mark --- 动画
- (void)springAnimation:(CGPoint)point{
    CASpringAnimation *anim = [CASpringAnimation animationWithKeyPath:@"bounds"];
    //阻尼系数，阻止弹簧伸缩的系数，阻尼系数越大，停止越快。默认值为10
    anim.damping = 100.0;
    //刚度系数(劲度系数/弹性系数)，刚度系数越大，形变产生的力就越大，运动越快。默认值为100
    anim.stiffness = 5000.0;
    //质量，影响图层运动时的弹簧惯性，质量越大，弹簧拉伸和压缩的幅度越大,动画的速度变慢.默认值为1
    anim.mass = 10.0;
    //初始速率，动画视图的初始速度大小,速率为正数时，速度方向与运动方向一致，速率为负数时，速度方向与运动方向相反
    anim.initialVelocity = 5.0f;
//    anim.fromValue = [NSValue valueWithCGPoint:CGPointMake(100, 150)];
    anim.toValue = [NSValue valueWithCGRect:CGRectMake(point.x, point.y, 100, 100)];
    //settlingDuration:结算时间返回弹簧动画到停止时的估算时间，根据当前的动画参数估算,通常弹簧动画的时间使用结算时间比较准确
    anim.duration = anim.settlingDuration;
    anim.delegate = self;
    anim.removedOnCompletion = NO;
    anim.fillMode = kCAFillModeForwards;
    anim.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut];
    anim.timeOffset = 0;
    anim.autoreverses = NO;
    [anim setValue:anim.toValue forKey:kSpringPositionKey];
    [self.redView.layer addAnimation:anim forKey:kSpringKey];
    
    

    
}

#pragma mark --- CAAnimationDelegate
- (void)animationDidStart:(CAAnimation *)anim{
    if ([self.redView.layer animationForKey:kSpringKey] == anim) {
        NSLog(@"动画开始");
    }
}
- (void)animationDidStop:(CAAnimation *)anim finished:(BOOL)flag{
    NSLog(@"----- %zd",flag);
    if ([self.redView.layer animationForKey:kSpringKey] == anim) {
        NSLog(@"动画结束");
        [CATransaction begin];
        [CATransaction setDisableActions:YES];
        self.redView.layer.position = [[anim valueForKey:kSpringPositionKey] CGPointValue];
        [CATransaction commit];
    }
}
#pragma mark --- 触摸
- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
    CGPoint point = [touches.anyObject locationInView:self.view];
    [self springAnimation:point];
    
}




@end
